<?php
	function __autoload($class_name)
	{
		include_once('../classes/'.strtolower($class_name).'.php');
	}
    include 'functions.php';
    
    $Loi = 'no';
    
    $HinhNho = "";
    $HinhTo = "";
    
    $HN = 'HinhNho';
    $pathsmall = "../images/sanpham/hinhnho/";
    uploadImages($HN, $pathsmall);
    
    for($i = 0; $i< count($_SESSION[$HN]); $i++)
        $HinhNho .= $_SESSION[$HN][$i]."|";        
    unset($_SESSION[$HN]);
    
    $HT = 'HinhTo';
    $pathbig = "../images/sanpham/hinhto/";
    uploadImages($HT, $pathbig);
    
    for($i = 0; $i< count($_SESSION[$HT]); $i++)
        $HinhTo .= $_SESSION[$HT][$i]."|";      
    
    if(isset($_SESSION['Loi']))
        $Loi = 'yes';
        
    $TenSanPham = $_POST['TenSanPham'];
    $GiaGoc = $_POST['GiaGoc'];    
    $GiaKhuyenMai = $_POST['GiaKhuyenMai'];
    $MoTa = $_POST['MoTa'];
    $ChiTiet = $_POST['ChiTiet'];
    $HuongDan = $_POST['HuongDan'];
    $SoLuong = $_POST['SoLuong'];
    $MaDonViTinh = $_POST['MaDonViTinh'];
    $MaQuyCach = $_POST['MaQuyCach'];    
    $MaXuatXu = $_POST['MaXuatXu'];
    $NgayNhap = date("Y-m-d H:i:s", time());
    
    $MaLoaiSanPham = $_POST['MaLoaiSanPham'];
    
    if(isset($_POST['MaLoaiSanPhamCap1']))
        $MaLoaiSanPhamCap1 = $_POST['MaLoaiSanPhamCap1'];
    else
        $MaLoaiSanPhamCap1 = 0;
    
    if(isset($_POST['MaLoaiSanPhamCap2']))
        $MaLoaiSanPhamCap2 = $_POST['MaLoaiSanPhamCap2'];
    else
        $MaLoaiSanPhamCap2 = 0;

    if(isset($_POST['MaLoaiSanPhamCap3']))
        $MaLoaiSanPhamCap3 = $_POST['MaLoaiSanPhamCap3'];
    else
        $MaLoaiSanPhamCap3 = 0;
    
    $sanpham = new sanphambs();
    $sanpham->TenSanPham = $TenSanPham;
    $sanpham->GiaGoc = $GiaGoc;
    $sanpham->GiaKhuyenMai = $GiaKhuyenMai;
    $sanpham->HinhNho = $HinhNho;
    $sanpham->HinhTo = $HinhTo;    
    $sanpham->MoTa = $MoTa;
    $sanpham->ChiTiet = $ChiTiet;
    $sanpham->HuongDan = $HuongDan;
    $sanpham->SoLuong = $SoLuong;
    $sanpham->MaDonViTinh = $MaDonViTinh;
    $sanpham->MaQuyCach = $MaQuyCach;
    $sanpham->MaXuatXu = $MaXuatXu;
    $sanpham->NgayNhap = $NgayNhap;
    $sanpham->MaLoaiSanPham = $MaLoaiSanPham;
    $sanpham->MaLoaiSanPhamCap1 = $MaLoaiSanPhamCap1;
    $sanpham->MaLoaiSanPhamCap2 = $MaLoaiSanPhamCap2;
    $sanpham->MaLoaiSanPhamCap3 = $MaLoaiSanPhamCap3;
    
    $isset = $sanpham->issetType();
    
    if($isset[0]['SoDong'] > 0)
        $Loi = 'isset';
    
    if($Loi == 'isset')
    {
        for($i = 0; $i< count($_SESSION[$HN]); $i++)
        {
            deleteFile($_SESSION[$HN][$i], $pathsmall);
        }
        for($i = 0; $i< count($_SESSION[$HT]); $i++)
        {
            deleteFile($_SESSION[$HT][$i], $pathbig);
        }
        echo "isset";
    }
    elseif(isset($_SESSION['Loi']))
    {
        for($i = 0; $i< count($_SESSION[$HN]); $i++)
        {
            deleteFile($_SESSION[$HN][$i], $pathsmall);
        }
        for($i = 0; $i< count($_SESSION[$HT]); $i++)
        {
            deleteFile($_SESSION[$HT][$i], $pathbig);
        }
        echo $_SESSION['Loi'];
        unset($_SESSION['Loi']);
    }
    else
    {
        if($sanpham->add())
            echo "yes";
        else
        {
            for($i = 0; $i< count($_SESSION[$HN]); $i++)
            {
                deleteFile($_SESSION[$HN][$i], $pathsmall);
            }
            for($i = 0; $i< count($_SESSION[$HT]); $i++)
            {
                deleteFile($_SESSION[$HT][$i], $pathbig);
            }
            echo "no";
        }
    }
    
    if(isset($_SESSION['Loi']))
        unset($_SESSION['Loi']);
        
    unset($_SESSION[$HN]);
    unset($_SESSION[$HT]);
?>